#include "company.h"

// 登陆界面
void Log_on(char *filename)
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return;
    }
    while (1)
    {
        char c;
        c = fgetc(fp);
        if (feof(fp) == 1) // 判断文件是否读到末尾
        {
            printf("\n");
            break;
        }
        printf("%c", c); // 打印读出的字符
    }
    fclose(fp);
}
// 管理员界面
void Administrator(char *filename)
{
    FILE *fp = fopen(filename, "r");
    while (1)
    {
        char c;
        c = fgetc(fp);
        if (feof(fp) == 1) // 判断文件是否读到末尾
        {
            printf("\n");
            break;
        }
        printf("%c", c); // 打印读出的字符
    }
    fclose(fp);
}
// 普通员工界面
void Staff(char *filename)
{
    FILE *fp = fopen("../ui/staff", "r");
    while (1)
    {
        char c;
        c = fgetc(fp);
        if (feof(fp) == 1) // 判断文件是否读到末尾
        {
            printf("\n");
            break;
        }
        printf("%c", c); // 打印读出的字符
    }
    fclose(fp);
}
// 创建节点
NODE *createNode(void *shuju)
{
    NODE *p = malloc(sizeof(NODE));
    p->shuju = shuju;
    p->xiayige = NULL;
    return p;
}
// 往链表中添加一个节点
void tianJiaJieDian(NODE *pTouJieDian, void *shuju)
{
    NODE *pNew = createNode(shuju);
    NODE *p = pTouJieDian;
    while (p->xiayige != NULL)
    {
        p = p->xiayige;
    }
    p->xiayige = pNew;
}
void insertNode(NODE *pTouJieDian, int weizhi, void *shuju)
{
    NODE *pNew = createNode(shuju); // 创建新的节点

    NODE *p = pTouJieDian;                   // p等于头结点
    while (weizhi > 0 && p->xiayige != NULL) // 位置大于0,
    {
        p = p->xiayige; // p往后移动
        weizhi--;       // 位置-1
    }

    pNew->xiayige = p->xiayige; // 让要插入的节点的下一个等于p（插入的位置的上一个）的下一个（要插入的位置）
    p->xiayige = pNew;          // p的下一个为新节点
}
// 从文件中加载用户到链表
void loadUserToList(NODE *pUserList, const char *filename)
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return;
    }
    USER user;
    while (fscanf(fp, "%s %s %s", user.account, user.password, user.number) == 3)
    {
        USER *newUser = (USER *)malloc(sizeof(USER));
        strcpy(newUser->account, user.account);
        strcpy(newUser->password, user.password);
        strcpy(newUser->number, user.number);
        tianJiaJieDian(pUserList, newUser);
    }
    fclose(fp);
}
// 注册
void zhuce()
{
    int flag;
    USER u;
    printf("选择你要注册的身份(1:管理员2:普通员工)");
    scanf("%d", &flag);

    printf("输入账号：");
    scanf("%s", u.account);
    printf("输入密码：");
    scanf("%s", u.password);
    printf("输入手机号：");
    scanf("%s", u.number);
    if (flag == 1)
    {
        SaveUser(&u, "../user/administrator");
        printf("注册成功\n");
    }
    else if (flag == 2)
    {
        SaveUser(&u, "../user/staff");
        printf("注册成功\n");
    }
}
// 保存用户结构体
void SaveUser(USER *puser, const char *filename)
{
    FILE *fp = fopen(filename, "aw");                                          // 打开文件用来写入信息
    fprintf(fp, "%s %s %s\n", puser->account, puser->password, puser->number); // 把用户结构体中的内容打印到文件中
    fclose(fp);
}
// 保存所有用户用于修改
void SaveAllUser(NODE *pUerList, const char *filename)
{
    FILE *fp = fopen(filename, "w");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return;
    }
    NODE *p = pUerList->xiayige; // 跳过头节点
    while (p != NULL)
    {
        USER *pUser = (USER *)p->shuju;
        fprintf(fp, "%s %s %s\n", pUser->account, pUser->password, pUser->number);
        p = p->xiayige;
    }
    fclose(fp);
}
// 管理员账号登录
int A_Login(NODE *pUserList, char *filename)
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return -1;
    }
    loadUserToList(pUserList, filename);
    USER A;
    printf("账号：");
    scanf("%s", A.account);
    printf("密码：");
    scanf("%s", A.password);
    USER fileuser;
    while (fscanf(fp, "%s %s %s", fileuser.account, fileuser.password, fileuser.number) == 3)
    {
        if (strcmp(A.account, fileuser.account) == 0 && strcmp(A.password, fileuser.password) == 0)
        {
            printf("登陆成功!!!\n");
            fclose(fp);
            return 1;
        }
    }
    fclose(fp);
    return 0;
}
// 普通员工账号登录
int S_Login(NODE *pUserList, char *filename)
{
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return -1;
    }
    loadUserToList(pUserList, filename);
    USER S;
    printf("账号：");
    scanf("%s", S.account);
    printf("密码：");
    scanf("%s", S.password);
    USER fileuser;
    while (fscanf(fp, "%s %s %s", fileuser.account, fileuser.password, fileuser.number) == 3)
    {
        if (strcmp(S.account, fileuser.account) == 0 && strcmp(S.password, fileuser.password) == 0)
        {
            printf("登陆成功!!!\n");
            fclose(fp);
            return 1;
        }
    }
    fclose(fp);
    return 0;
}
// 修改密码
int Change_password(NODE *pUserList)
{
    int chioce;
    char account[128];
    char password[128];
    USER *u = malloc(sizeof(USER));

    printf("输入你要修改的用户(1:管理员2:普通员工):");
    scanf("%d", &chioce);

    const char *filename = (chioce == 1) ? "../user/administrator" : "../user/staff";
    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return -1;
    }
    printf("输入你的账号：");
    scanf("%s", account);
    printf("输入你的密码：");
    scanf("%s", password);

    loadUserToList(pUserList, filename);
    NODE *p = pUserList->xiayige;
    while (p != NULL)
    {
        USER *fileuser = (USER *)p->shuju;
        // printf("文件中账号:%s,输入的账号：%s", fileuser->account, account);//测试打印文件内容是否加载成功
        if (strcmp(fileuser->account, account) == 0 && strcmp(fileuser->password, password) == 0)
        {
            printf("输入新的密码：");
            scanf("%s", u->password);
            // 修改密码
            strcpy(fileuser->password, u->password);
            // 保存所有用户信息到文件
            SaveAllUser(pUserList, filename);
            printf("密码修改成功！！！\n");
            fclose(fp);
            free(u);
            return 1;
        }
        p = p->xiayige;
    }
    printf("账号或密码错误，修改失败!!!!\n");
    fclose(fp);
    free(u);
    return 0;
}
// 找回密码
int Retrieve_password(NODE *pUserList)
{
    char account[128];
    char number[128];
    int chioce;
    printf("输入你要找回的账号类型(1:管理员2:普通员工)：");
    scanf("%d", &chioce);
    const char *filename = (chioce == 1) ? "../user/administrator" : "../user/staff";

    FILE *fp = fopen(filename, "r");
    if (fp == NULL)
    {
        perror("文件打开失败");
        return -1;
    }
    printf("输入你的账号：");
    scanf("%s", account);
    printf("输入你的号码：");
    scanf("%s", number);

    loadUserToList(pUserList, filename);
    NODE *p = pUserList->xiayige;
    while (p != NULL)
    {
        USER *fileuser = (USER *)p->shuju;
        if (strcmp(fileuser->account, account) == 0 && strcmp(fileuser->number, number) == 0)
        {
            printf("你的密码是：%s\n", fileuser->password);
            fclose(fp);
            return 1;
        }
        p = p->xiayige;
    }
    printf("账号或号码错误，找回密码失败！！！\n");
    fclose(fp);
    return 0;
}
